package top.shiyiri.reflection3;

import org.junit.Test;
import top.shiyiri.reflection2.Person;

import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

/**
 * @author Aunean
 * @date 2022/1/12
 */
public class OtherTest {

    /*
    获取构造器结构
     */

    @Test
    public void test1() {
        Class<Person> clazz = Person.class;

        //getConstructors()：获取当前运行时类中声明为public的构造器
        Constructor<?>[] constructors1 = clazz.getConstructors();
        for (Constructor constructor: constructors1) {
            System.out.println(constructor);
            //public top.shiyiri.reflection2.Person()
        }

        System.out.println("******************************");

        //getConstructors()：获取当前运行时类中声明的所有构造器
        Constructor<?>[] constructors = clazz.getDeclaredConstructors();
        for (Constructor<?> constructor: constructors) {
            System.out.println(constructor);
            /*
            top.shiyiri.reflection2.Person(java.lang.String,int)
            private top.shiyiri.reflection2.Person(java.lang.String)
            public top.shiyiri.reflection2.Person()
             */
        }
    }

    /*
    获取运行时类的父类
     */
    @Test
    public void test2() {
        Class<Person> clazz = Person.class;

        Class<? super Person> superclass = clazz.getSuperclass();
        //class top.shiyiri.reflection2.Creature
        System.out.println(superclass);
    }

    /*
    获取运行时类的带泛型的父类
     */
    @Test
    public void test3() {
        Class<Person> clazz = Person.class;

        Type genericSuperclass = clazz.getGenericSuperclass();
        //top.shiyiri.reflection2.Creature<java.lang.String>
        System.out.println(genericSuperclass);
    }

    /*
    获取运行时类的带泛型的父类的泛型
     */
    @Test
    public void test4() {
        Class<Person> clazz = Person.class;

        Type genericSuperclass = clazz.getGenericSuperclass();
        ParameterizedType parameterizedType = (ParameterizedType) genericSuperclass;

        //获取泛型类型
        Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
        for (Type a: actualTypeArguments) {
            System.out.println(a);//class java.lang.String
            System.out.println(a.getTypeName());//java.lang.String
            System.out.println(((Class) a).getName());//java.lang.String
        }
    }

    /*
    获取运行时类实现的接口
     */
    @Test
    public void test5() {
        Class clazz = Person.class;

        Class[] interfaces = clazz.getInterfaces();
        for (Class c: interfaces) {
            System.out.println(c);
            /*
            interface java.lang.Comparable
            interface top.shiyiri.reflection2.MyInterface
             */
        }

        System.out.println();

        //获取运行时类父类实现的接口
        Class[] interfaces1 = clazz.getSuperclass().getInterfaces();
        for (Class in: interfaces1) {
            System.out.println(in);
            //interface java.io.Serializable
        }
    }

    /*
    获取运行时类所在的包
     */
    @Test
    public void test6() {
        Class clazz = Person.class;

        Package pack = clazz.getPackage();
        System.out.println(pack);//package top.shiyiri.reflection2
    }

    /*
    获取运行时类声明的注解
     */
    @Test
    public void test7() {
        Class clazz = Person.class;

        Annotation[] annotations = clazz.getAnnotations();
        for (Annotation annotation: annotations) {
            System.out.println(annotation);
        }
    }
}
